﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;






namespace WPF.Element.UI.CustomControls
{

    public class ElPassWord : Control
    {

        static ElPassWord()
        {
            //覆盖默认的样式
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ElPassWord), new FrameworkPropertyMetadata(typeof(ElPassWord)));
        }

        // 定义一个依赖属性来存储密码文本
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register(
                "Password",
                typeof(string),
                typeof(ElPassWord),
                new PropertyMetadata(string.Empty, OnPasswordChanged));
        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value); }
        }
        /// <summary>
        /// Password 属性值变化的时候
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // 当密码改变时，可以在这里添加逻辑


        }


        // 创建一个TextBox来实际输入密码
        private TextBox _textBox;
        private PasswordBox _passwordBox;
        private Button _myButton;
        /// <summary>
        /// 自定义控件中控件被初始化
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            // 获取模板中的TextBox      这个是在控件模版元素中的控件名称
            _textBox = Template.FindName("PART_PasswordTextBox", this) as TextBox;
            if (_textBox != null)
            {
                // _textBox.PasswordChar = '*'; // 设置密码字符
                _textBox.TextChanged += OnTextBoxTextChanged;
            }

            //显示或者影藏密码
            _myButton = GetTemplateChild("PART_ShowPassWordButtonClick") as Button;
            if (_myButton != null)
            {
                // 绑定按钮的点击事件
                _myButton.Click += MyButton_Click;
            }

            _passwordBox = GetTemplateChild("PART_PasswordBox") as PasswordBox;
            if (_passwordBox != null)
            {
                // 绑定按钮的点击事件
                _passwordBox.PasswordChanged += PasswordChangedEvent;

            }


        }

        /// <summary>
        /// 密码啊花生变化是的时候
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private  void PasswordChangedEvent(object sender, RoutedEventArgs e)
        {
            var passwordBox =sender as PasswordBox;
            if (_textBox.Visibility == Visibility.Hidden)
            {
                _textBox.Text = passwordBox.Password;
                Password = passwordBox.Password;
            }
        }


        /// <summary>
        /// 输入的内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            if (_passwordBox.Visibility == Visibility.Hidden)
            {
                _passwordBox.Password = _textBox.Text;
                Password = _textBox.Text;
            }
        }

        /// <summary>
        /// 圆角大小
        /// </summary>
        public static readonly DependencyProperty ElCornerRadiusProperty =
            DependencyProperty.Register("ElCornerRadius", typeof(CornerRadius), typeof(ElPassWord));   // new PropertyMetadata(0, null)

        /// <summary>
        /// 圆角 设置这个属性可以直接设置圆角
        /// </summary>
        public CornerRadius ElCornerRadius
        {
            get
            {
                return (CornerRadius)GetValue(ElCornerRadiusProperty);
            }
            set
            {
                SetValue(ElCornerRadiusProperty, value);
            }
        }



        /// <summary>
        /// 输入框选中边框颜色
        /// </summary>
        public static readonly DependencyProperty FocusedBorderBrushProperty =
            DependencyProperty.Register("FocusedBorderBrush", typeof(Brush), typeof(ElPassWord));   // new PropertyMetadata(0, null)

        /// <summary>
        /// 输入框选中边框颜色
        /// </summary>
        public Brush FocusedBorderBrush
        {
            get
            {
                return (Brush)GetValue(FocusedBorderBrushProperty);
            }
            set
            {
                SetValue(FocusedBorderBrushProperty, value);
            }
        }

        /// <summary>
        /// 提示输入内容
        /// </summary>
        public static readonly DependencyProperty PlaceholderTextProperty =
            DependencyProperty.Register("PlaceholderText", typeof(string), typeof(ElPassWord));   // new PropertyMetadata(0, null)

        /// <summary>
        /// 提示输入内容
        /// </summary>
        public string PlaceholderText
        {
            get
            {
                return (string)GetValue(PlaceholderTextProperty);
            }
            set
            {
                SetValue(PlaceholderTextProperty, value);
            }
        }

        /// <summary>
        /// 提示信息字體大小
        /// </summary>
        public static readonly DependencyProperty PlaceholderTextFontSizeProperty =
            DependencyProperty.Register("PlaceholderTextFontSize", typeof(double), typeof(ElPassWord));   // new PropertyMetadata(0, null)
        public double PlaceholderTextFontSize
        {
            get
            {
                return (double)GetValue(PlaceholderTextFontSizeProperty);
            }
            set
            {
                SetValue(PlaceholderTextFontSizeProperty, value);
            }
        }


        /// <summary>
        /// 切换是否明文显示密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyButton_Click(object sender, RoutedEventArgs e)
        {
            // 处理按钮点击事件
            // MessageBox.Show("Button clicked!");
            if (_passwordBox.Visibility == Visibility.Hidden)
            {
                _passwordBox.Visibility = Visibility.Visible;
                _textBox.Visibility = Visibility.Hidden;
            }
            else
            {
                _passwordBox.Visibility = Visibility.Hidden;
                _textBox.Visibility = Visibility.Visible;
            }
        }
    }
}
